package cn.com.hfuu.service;

import java.io.InputStream;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import cn.com.hfuu.ExcelUtil;
import cn.com.hfuu.UUIdUtil;
import cn.com.hfuu.dao.landMapper;
import cn.com.hfuu.dao.treeMapper;
import cn.com.hfuu.dao.userMapper;
import cn.com.hfuu.module.land;
import cn.com.hfuu.module.tree;
import cn.com.hfuu.module.user;

@Service("treeservice")
public class TreeService {

	@Autowired
	treeMapper treemap;
	
	@Autowired
	landMapper landMapper;
	
	@Autowired
	userMapper userMapper;
	
	/**
	 * 获取分页
	 * @param start
	 * @param limit
	 * @param tree
	 * @return
	 */
	public List<cn.com.hfuu.module.tree> selectpage(Integer start,
			Integer limit, tree tree) {
		// TODO Auto-generated method stub
		return treemap.selectpage(start,limit,tree.getTreeId(),tree.getTreeLand(),tree.getTreeState());
	}

	
	/**
	 * 获取分页数
	 * @param tree
	 * @return
	 */
	public List<tree> selectpagetotal(tree tree) {
		// TODO Auto-generated method stub
		return treemap.selectpagetotal(tree.getTreeId(), tree.getTreeLand(), tree.getTreeState());
	}


	/**
	 * 添加处理
	 * @param t
	 * @return
	 */
	public int insert(tree t) {
		// TODO Auto-generated method stub
		return treemap.insertSelective(t);
	}

	/**
	 * 通过主键查信息
	 * @param id
	 * @return
	 */
	public tree selectbyid(String id) {
		// TODO Auto-generated method stub
		return treemap.selectByPrimaryKey(id);
	}


	/**
	 * 更新茶树信息
	 * @param t
	 * @return
	 */
	public int upadte(tree t) {
		// TODO Auto-generated method stub
		return treemap.updateByPrimaryKeySelective(t);
	}


	public int delete(String[] ids) {
		// TODO Auto-generated method stub
		return treemap.deletes(ids);
	}


	@Transactional
	public  void upload(InputStream in, MultipartFile file) throws Exception {
		// TODO Auto-generated method stub
		List<List<Object>> listob = ExcelUtil.getBankListByExcel(in,
				file.getOriginalFilename());
		UUIdUtil idu=new UUIdUtil();
		for (int i=0;i<listob.size();i++) {
			List<Object> ob = listob.get(i);
			System.out.println(ob.toString());
			tree t=new tree();
			t.setTreeId(idu.getUuid());
			t.setTreeVarity(ob.get(0).toString().trim());
			land l=landMapper.selectbyname(ob.get(1).toString().trim());
			if(l==null){
				throw new RuntimeException("第"+(i+1)+"行茶园名不存在");
			}
			String landid=l.getLandId();
			t.setTreeLand(landid);
			String state=String.valueOf(ob.get(2)).trim();
			if(state.equals("正常")){
				t.setTreeState(1);
			}else if(state.equals("异常")){
				t.setTreeState(2);
			}else if(state.equals("坏死")){
				t.setTreeState(0);
			}else{
				throw new RuntimeException("第"+(i+1)+"行状态错误");	
			}
			user u=userMapper.selectByUsername(String.valueOf(ob.get(3)).trim());
			if(u==null){
				throw new RuntimeException("第"+(i+1)+"行栽种人不存在");
			}
			t.setTreeStartuser(u.getUserId());
			t.setTreeStarttime(String.valueOf(ob.get(4)).trim());
			treemap.insertSelective(t);
			
		}
	} 

}
